package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Subject;
import com.oa.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@CrossOrigin
@ResponseBody
@Controller
public class SubjectConntroller {

    @Autowired
    SubjectService subjectService;

    @RequestMapping(value = "/getSubjectPage")
    public Result getSubjectPage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }
        Page subjectPage = subjectService.getSubjectPage(pageNo, pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setMessage(message);
        result.setData(subjectPage);
        return result;
    }

    @RequestMapping(value = "/getSubjectList")
    public Result getSubjectList(){
        List<Subject> subjectList = subjectService.getSubjectList();
        Message message = new Message();
        Result result = new Result();
        result.setMessage(message);
        result.setData(subjectList);
        return result;
    }

    @RequestMapping(value = "/getSubject")
    public Result getSubject(Integer subjectId){
        Subject subject = subjectService.getSubject(subjectId);
        System.out.println(subject);
        Message message = new Message();
        Result result = new Result();
        result.setMessage(message);
        result.setData(subject);
        return result;
    }

    @RequestMapping(value = "/addSubject")
    public Result addSubject(@RequestBody Subject subject){
        System.out.println(subject);
        Date date = new Date();
        subject.setCreateTime(date);
        subjectService.addSubject(subject);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/delSubject")
    public Result delSubject(Integer subjectId){
        System.out.println(subjectId);
        subjectService.delSubject(subjectId);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/updateSubject")
    public Result updateSubject(@RequestBody Subject subject){
        System.out.println(subject);
        subjectService.updateSubject(subject);
        Result result = new Result();
        return result;
    }

}
